package org.example.study4;

/**
 * 使用字符串常量作为锁（危险！！）
 */
public class Demo_412 {
    public static void main(String[] args) throws InterruptedException {
        Counter412 counter = new Counter412();
        Counter412 counter1 = new Counter412();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter1.increase();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("count = " + Counter412.count);
    }
}

class Counter412 {
    public static int count = 0;

    public void increase() {
        // 使用字符串常量作为锁
        synchronized ("GLOBAL_LOCK") {
            count++;
        }
    }
}
// 运行结果：count = 100000